package com.guducat.collegeWeb.service;

import com.guducat.collegeWeb.dto.response.*;
import com.guducat.collegeWeb.entity.*;
import com.guducat.collegeWeb.mapper.HomeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HomeService {
    private final HomeMapper homeMapper;

    public HomeService(HomeMapper homeMapper) {
        this.homeMapper = homeMapper;
    }

    // HomeSlide相关方法
    public List<HomeSlideDTO> getActiveHomeSlides() {
        try {
            List<HomeSlide> slides = homeMapper.getActiveHomeSlides();
            return slides.stream()
                    .map(this::convertToHomeSlideDTO)
                    .collect(Collectors.toList());
        } catch (Exception e) {
            // 当数据库表不存在或查询出错时返回空列表
            System.out.println("Error getting active home slides: " + e.getMessage());
            return List.of();
        }
    }

    private HomeSlideDTO convertToHomeSlideDTO(HomeSlide slide) {
        HomeSlideDTO dto = new HomeSlideDTO();
        dto.setId(slide.getId());
        dto.setTitle(slide.getTitle());
        dto.setDescription(slide.getDescription());
        dto.setImage(slide.getImageUrl());
        dto.setLink(slide.getLinkUrl());
        return dto;
    }

    // CampusHighlight相关方法
    public List<CampusHighlightDTO> getActiveCampusHighlights() {
        try {
            List<CampusHighlight> highlights = homeMapper.getActiveCampusHighlights();
            return highlights.stream()
                    .map(this::convertToCampusHighlightDTO)
                    .collect(Collectors.toList());
        } catch (Exception e) {
            // 当数据库表不存在或查询出错时返回空列表
            System.out.println("Error getting active campus highlights: " + e.getMessage());
            return List.of();
        }
    }

    // AcademicResearch相关方法
    public List<AcademicResearchDTO> getActiveAcademicResearches() {
        try {
            List<AcademicResearch> researches = homeMapper.getActiveAcademicResearches();
            return researches.stream()
                    .map(this::convertToAcademicResearchDTO)
                    .collect(Collectors.toList());
        } catch (Exception e) {
            System.out.println("Error getting active academic researches: " + e.getMessage());
            return List.of();
        }
    }

    private AcademicResearchDTO convertToAcademicResearchDTO(AcademicResearch research) {
        AcademicResearchDTO dto = new AcademicResearchDTO();
        dto.setId(research.getId());
        dto.setTitle(research.getTitle());
        dto.setDescription(research.getDescription());
        dto.setImageUrl(research.getImageUrl());
        dto.setLinkUrl(research.getLinkUrl());
        return dto;
    }

    private CampusHighlightDTO convertToCampusHighlightDTO(CampusHighlight highlight) {
        CampusHighlightDTO dto = new CampusHighlightDTO();
        dto.setId(highlight.getId());
        dto.setTitle(highlight.getTitle());
        dto.setCategory(highlight.getCategory());
        dto.setImage(highlight.getImageUrl());
        dto.setDate(highlight.getDate());
        dto.setLink(highlight.getLinkUrl());
        return dto;
    }

    // MiddleSlide相关方法
    public List<MiddleSlideDTO> getActiveMiddleSlides() {
        try {
            List<MiddleSlide> slides = homeMapper.getActiveMiddleSlides();
            return slides.stream()
                    .map(this::convertToMiddleSlideDTO)
                    .collect(Collectors.toList());
        } catch (Exception e) {
            System.out.println("Error getting active middle slides: " + e.getMessage());
            return List.of();
        }
    }

    private MiddleSlideDTO convertToMiddleSlideDTO(MiddleSlide slide) {
        MiddleSlideDTO dto = new MiddleSlideDTO();
        dto.setId(slide.getId());
        dto.setTitle(slide.getTitle());
        dto.setSubtitle(slide.getSubtitle());
        dto.setContent(slide.getContent());
        dto.setBackgroundImage(slide.getBackgroundImage());
        return dto;
    }

    // HeroSection相关方法
    public HeroSectionDTO getActiveHeroSection() {
        try {
            HeroSection heroSection = homeMapper.getActiveHeroSection();
            return heroSection != null ? convertToHeroSectionDTO(heroSection) : null;
        } catch (Exception e) {
            System.out.println("Error getting active hero section: " + e.getMessage());
            return null;
        }
    }

    private HeroSectionDTO convertToHeroSectionDTO(HeroSection heroSection) {
        HeroSectionDTO dto = new HeroSectionDTO();
        dto.setId(heroSection.getId());
        dto.setMainTitle(heroSection.getMainTitle());
        dto.setHighlightText(heroSection.getHighlightText());
        dto.setSubTitle(heroSection.getSubTitle());
        dto.setButton1Text(heroSection.getButton1Text());
        dto.setButton1Link(heroSection.getButton1Link());
        dto.setButton2Text(heroSection.getButton2Text());
        dto.setButton2Link(heroSection.getButton2Link());
        return dto;
    }



    // HeroOption相关方法
    public List<HeroOptionDTO> getActiveHeroOptions() {
        try {
            List<HeroOption> options = homeMapper.getActiveHeroOptions();
            return options.stream()
                    .map(this::convertToHeroOptionDTO)
                    .collect(Collectors.toList());
        } catch (Exception e) {
            System.out.println("Error getting active hero options: " + e.getMessage());
            return List.of();
        }
    }

    private HeroOptionDTO convertToHeroOptionDTO(HeroOption option) {
        HeroOptionDTO dto = new HeroOptionDTO();
        dto.setId(option.getId());
        dto.setTitle(option.getTitle());
        dto.setDescription(option.getDescription());
        dto.setImage(option.getImageUrl());
        return dto;
    }

    // HeroStat相关方法
    public List<HeroStatDTO> getActiveHeroStats() {
        try {
            List<HeroStat> stats = homeMapper.getActiveHeroStats();
            return stats.stream()
                    .map(this::convertToHeroStatDTO)
                    .collect(Collectors.toList());
        } catch (Exception e) {
            System.out.println("Error getting active hero stats: " + e.getMessage());
            return List.of();
        }
    }

    private HeroStatDTO convertToHeroStatDTO(HeroStat stat) {
        HeroStatDTO dto = new HeroStatDTO();
        dto.setId(stat.getId());
        dto.setNumber(stat.getNumber());
        dto.setSuffix(stat.getSuffix());
        dto.setLabel(stat.getLabel());
        return dto;
    }
}
